package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import org.apache.log4j.Logger;
import controller.RegistController;

public class RegistWin extends JFrame{
	
	public static RegistWin current;
	private static final Logger LOGGER = Logger.getLogger(LoginWin.class);
	private static final long serialVersionUID = 1L;
	
	private JTextField nameTf;
	private JPasswordField pwdPf;
	private JPasswordField pwdPf2;
	private JButton registBtn;
	private JLabel nameLa;
	private JLabel pwdLa;
	private JLabel pwdConfirmLa;
	
	public RegistWin(){
		this.setSize(400, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("IM Regist");
		
		current = this; 
		
		init();
		addEvent();
		this.setVisible(false);
	}

	public JTextField getNameTf() {
		return nameTf;
	}

	public void setNameTf(JTextField nameTf) {
		this.nameTf = nameTf;
	}

	public JPasswordField getPwdPf() {
		return pwdPf;
	}

	public void setPwdPf(JPasswordField pwdPf) {
		this.pwdPf = pwdPf;
	}

	private void init(){
		this.setLayout(null);
		nameTf = new JTextField();
		pwdPf = new JPasswordField();
		pwdPf2 = new JPasswordField();
		registBtn = new JButton("Regist");
		nameLa = new JLabel("User");
		pwdLa = new JLabel("Password");
		pwdConfirmLa = new JLabel("Confirm");
		
		nameTf.setBounds(100, 50, 260, 30);
		pwdPf.setBounds(100, 110, 260, 30);
		pwdPf2.setBounds(100, 170, 260, 30);
		
		registBtn.setBounds(210, 220, 90, 30);
		nameLa.setBounds(30, 50, 60, 30);
		pwdLa.setBounds(30, 110, 60, 30);
		pwdConfirmLa.setBounds(30, 170, 60, 30);
		
		this.add(nameTf);
		this.add(pwdPf);
		this.add(registBtn);
		this.add(nameLa);
		this.add(pwdLa);
		this.add(pwdPf2);
		this.add(pwdConfirmLa);
	}
	private void addEvent(){
		
		nameTf.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				}
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		
		pwdPf.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				}
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		
		registBtn.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				String name = nameTf.getText();
				String pwd = new String(pwdPf.getPassword());
				String pwdConfirm = new String(pwdPf2.getPassword());
				if(pwd.equals(pwdConfirm)){
					try {
						new RegistController().control(name, pwd);
					} catch (IOException e1) {
						LOGGER.error(e1.toString());
						JOptionPane.showMessageDialog(RegistWin.this, "The server is crashed!", "Error", JOptionPane.ERROR_MESSAGE);
					}
					nameTf.setText(null);
					pwdPf.setText(null);
				}else{
					JOptionPane.showMessageDialog(RegistWin.this, "Confirm your password", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
	}
	
}
